<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :offset="6">
        <el-form
          class="login_form"
          ref="formRef"
          :model="loginForm"
          :rules="rules"
        >
          <h1>Hello&nbsp;!</h1>
          <h2>欢迎来到管理平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              placeholder="账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login_btn"
              :loading="loading"
              :loading-icon="Loading"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock, Loading } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { reqLogin } from '@/api/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.ts'
// import { NotificationEmits } from 'element-plus'
// 定义表单数据
const loginForm = ref({ username: 'admin', password: '111111' })
// 定义加载变量
const loading = ref<boolean>(false)
// 配置校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'change' },
    {
      pattern: /^\S{5,10}$/g,
      message: '用户名必须是5-10的非空字符',
      trigger: 'change'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'change' },
    {
      pattern: /^\S{6,10}$/g,
      message: '用户名必须是6-10的非空字符',
      trigger: 'change'
    }
  ]
}
// 获取user仓库
const userStore = useUserStore()
// 获取路由
const router = useRouter()
// 获取表单元素
const formRef = ref()
const login = async () => {
  // 登陆前预校验
  await formRef.value.validate()
  // 按钮加载效果
  loading.value = true
  await reqLogin(loginForm.value)
    .then((res) => {
      if (res.code === 200) {
        userStore.setToken(res.data.token)
        loading.value = false
        router.push('/')
        ElNotification({
          type: 'success',
          message: '欢迎回来',
          title: `Hi,${getTime()}好`,
          duration: 1500
        })
      } else {
        return Promise.reject(res)
      }
    })
    .catch((error) => {
      loading.value = false
      ElNotification({
        type: 'error',
        message: error.data.message,
        duration: 1500
      })
    })
}
</script>

<style scoped lang="scss">
.login_container {
  position: relative;
  width: 100%;
  height: 100vh;
  background: linear-gradient(to right, #33d4ff, #edd3ff);
  .login_form {
    position: absolute;
    width: 500px;
    left: 50%;
    top: 50vh;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.1);
    padding: 40px 60px 60px 60px;
    backdrop-filter: blur(5px);
    border-radius: 20px;
    text-align: center;
    h1 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    h2 {
      color: white;
      font-size: 15px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
